$(function(){ 
	$('.table-sort').dataTable(pageStting(baseURL));
	$('#DataTables_Table_0_length').find('label').contents().wrap("<span class='f-l mr-10'></span>");
	$(window).resize(function(){
		$(".table-sort").css('width','100%');
	})
});
// 从服务器分批取数据
 pageStting =  function(baseURL){
		var optsortable = {
					"bPaginate":true,
					"bProcessing":true,
					"stateSave":true,
					"iDisplayLength":10,
					"sAjaxSource":baseURL+'/api/drugs/list' +"?t="+new Date().getTime(),
					"sPaginationType":'full_numbers',
					"lengthMenu":[10,25],
					"bServerSide": true,
					"bSort": false,             
					"bFilter": false,  
				     "aoColumnDefs": [  
				                      {
				                    	"bVisible":false,
				                    	"aTargets":[0]
				                      },
				                      {
					                    	"render":function(data,type,full){
					                    		if(full[5]!="/"){
					                    			return full[5];
					                    		}else return "/"
					                    	},
					                    	"aTargets":[5]
					                   },
					                   {               
				                            "render": function (data, type, full){  
				                            	var edit_herf ="<div class='text-c handlePointer'><img title='编辑用户' alt='编辑用户' " +
				                            			"src='"+baseURL+"/images/admin/cp_edite.png' onclick=layer_show('850','','编辑用户','"+baseURL+"/api/drugs/page/update/"+full[0]+"') />"
				    							var trach_herf="<img title='删除' alt='删除' src='"+baseURL+"/images/admin/cp_delete.png' onclick=\"return deleteData(null,'"+full[0]+"')\" /></div>"
				                            	return edit_herf+trach_herf;
				                              },
				                            "targets":13
				                           }
					                  
				                   ] ,
					"fnServerData": function(sSource, aoData, fnCallback){
							var globalSearch = {};
							var searchConditions = {};
							if($.trim($("#userName").val())!=''){
								searchConditions.queryName=$.trim($("#userName").val());
							}
							if(null!=$("#projectSelect").val() && $("#projectSelect").val()!='' && $("#projectSelect").val()!=' ' && 'null'!=$("#projectSelect").val() ){
								searchConditions.progressTypeCode=$("#projectSelect").val();
							}
							globalSearch.name= 'globalSearch';
							globalSearch.value= JSON.stringify(searchConditions);
							aoData.push(globalSearch);
							 $.ajax({  
						        type: "GET",     
						        url: sSource,     
						        dataType: "json",    
						        contentType: "application/json;charset=UTF-8", 
						        data: aoData, // 以json格式传递
						        success: function(result) {
					    			if(!result.flag){
					            		if(result.statusCode==503){
					            		  location=baseURL+"/api/tologin";
					            		}else{
					            		  layer.msg("查询失败！");
					            		}
					            	  return;
					            	}
						        	if(result.flag){
							        	var obj =result;
										var data = obj.results;
										var aaData = [];
										var dataResult = {};
										dataResult.iTotalRecords = obj.totalcount;
										dataResult.iTotalDisplayRecords= obj.totalcount;
										dataResult.iDisplayStart = obj.offset;
										dataResult.iDisplayLength= obj.limit;
										dataResult.aaData = aaData;
										// json重构数组对象
										$.each(data,function(i,val){
											aaData[i]=[];
											aaData[i].push(val.id==null?'/':val.id);
											aaData[i].push(projectTest(val.id));
											aaData[i].push(projectTest(val.drugName));
											aaData[i].push(projectTest(val.commonName));
											aaData[i].push(projectTest(val.englishName));
											aaData[i].push(projectTest(val.chineseSpell));
											aaData[i].push(projectTest(val.character));
											aaData[i].push(projectTest(val.indication));
											aaData[i].push(val.element);
											aaData[i].push(projectTest(val.usageDosage));
											aaData[i].push(projectTest(val.taboo));
											aaData[i].push(projectTest(val.attention));
											aaData[i].push(projectTest(val.drugInteraction));
											aaData[i].push(val.null_n);
										});
							            fnCallback(dataResult);   
						          }
						        }    
						     });
					}
				}	
		return optsortable;
}		
 deleteData = function(selName,id){
		 
		if(id==""){
			layer.msg("请选择删除项！");
			return false;
		}
		var data = {};
		layer.confirm("您确定要删除吗？",function(){
			$.ajax({  
		        type: "post",     
		        url: '../deleteDrugs/'+id,     
		        dataType: "json",   
		        contentType: "application/json",
		        data: JSON.stringify(data),
		        success: function(result) {
		        	   if(!result.flag){
		        		   layer.msg("删除失败！原因："+result.message);
		        	   }else{
		        		   location.reload();
		        	   }
		        }    
		     });
		})
		return true;
		}
doSearch = function(){
	   var seachValue={};
	   $('.table-sort').DataTable().search(seachValue).draw();
}
projectTest=function(obj){
	if(obj=="" || obj==null){
		return "/";
	}else return obj
}
doReset=function(){
	$('#userName').val('');
}
function engineerSource(obj){
	if(obj=="1"){
		return "论坛";
	}else return "咨询";
}